package db_guess

import (
	"errors"
	global2 "longmen/server/config/global"
	"longmen/server/pkg/db/mysql"
	"longmen/server/pkg/db/mysql/models/models_guess"
)

func InsertQuestion(question *models_guess.Question) (int, error) {
	fName := "InsertQuestion"
	return GuessInsertAny(question, fName)
}

/*
*
@Create 2022 July 19 Enzo
对于question来说，只能update：AuthByUid审批人， 状态(是否已审核status)，审核原因reason
*/
func UpdateQuestionByColumns(columns []string, question *models_guess.Question) error {
	err := global2.Guess().Select(columns).Updates(question).Error
	if err != nil {
		return err
	}
	return nil
}

// 看问题是这个主播的吗
func CheckAnchorQuestion(anchorId int, qid int) (*models_guess.Question, error) {
	q, err := GetQuestionParam(map[string]interface{}{"anchor_id": anchorId, "id": qid})
	if err != nil {
		return nil, err
	}
	if q == nil || q.Id == 0 {
		return nil, errors.New("params error")
	}
	return q, nil
}

func GetQuestionParam(param map[string]interface{}) (*models_guess.Question, error) {
	o := &models_guess.Question{}
	table := mysql.GetGuessTable(&models_guess.Question{})
	mysql.Condition(table, param, "", "", 0, 0)
	err := table.Find(o).Error
	if err != nil {
		return nil, err
	}
	return o, err
}
